using DA.AnimGraph;
using System;


namespace DA.AnimGraph.Editor {
    public class PreviewEnv {

        public bool isConnecting => connection != null;
        public AGEPreviewConnection connection { private set; get; }
        public AGTree agTree;

        public event Action<AGEPreviewConnection> OnConnect;
        public event Action<AGEPreviewConnection> ConnectUpdate;
        public event Action OnDisconnect;

        public void Connect(string goName) {
            var player = AGEGlobal.GetInstance().preview.GetPlayer(goName);
            if (player == null) return;
            connection = new AGEPreviewConnection(player);
            OnConnect?.Invoke(connection);
        }
        public void Disconnect() {
            connection.Dispose();
            connection = null;
            OnDisconnect?.Invoke();
        }
        public void AddGraphToConnectionByContext(AGEContext context, string slotName) {
            connection.AddGraph(context.titleText,
                context.ExportAgpAsset(),
                context.resourceManager.DumpToDict(),
                context.GetBlackboardInfo(),
                slotName
            );
            ConnectUpdate?.Invoke(connection);
        }
    }
}
